package presale_house.show.view;

import presale_house.entity.ClientDo;
import presale_house.entity.HouseDo;
import presale_house.handler.UpdateClientViewHandler;
import presale_house.service.ClientService;
import presale_house.service.impl.ClientServiceImpl;
import presale_house.show.MainView;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

/**
 * 更新客户信息界面
 */
public class UpdateClientView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel idLabel=new JLabel("编号",JLabel.RIGHT);
    JTextField idText=new JTextField();
    JLabel nameLabel=new JLabel("姓名",JLabel.RIGHT);
    JTextField nameText=new JTextField();
    JLabel sexLabel=new JLabel("性别",JLabel.RIGHT);
  //  JTextField sexText=new JTextField();
    JComboBox sex;
    JLabel phoneLabel=new JLabel("电话",JLabel.RIGHT);
    JTextField phoneText =new JTextField();
    JLabel addressLabel=new JLabel("地址",JLabel.RIGHT);
    JTextField addressText =new JTextField();
    JLabel ideal_houseLabel =new JLabel("理想户型",JLabel.RIGHT);
  //  JTextField idea_houseText =new JTextField();
  JComboBox ideal_house;

    JLabel select_numLabel=new JLabel("预售选号",JLabel.RIGHT);
    JTextField select_numText =new JTextField();
    JButton updateBtn=new JButton("修改");
    HouseDo houseDo =new HouseDo();

    UpdateClientViewHandler updateClientViewHandler;



    public UpdateClientView(MainView mainView, int selectedClientId){
        super(mainView,"修改客户信息",true);
        updateClientViewHandler =new UpdateClientViewHandler(this,mainView);
        //查询selectedClientIds对应的记录并回显
        ClientService clientService =new ClientServiceImpl();
        ClientDo selectedClient= clientService.getById(selectedClientId);



        //添加组件
        idLabel.setPreferredSize(new Dimension(80,30));
       idText.setPreferredSize(new Dimension(200,30));
        jPanel.add(idLabel);
        idText.setText(selectedClient.getId()+"");
        //设置Id不可编辑
        idText.setEnabled(false);
        jPanel.add(idText);

        nameLabel.setPreferredSize(new Dimension(80,30));
        nameText.setPreferredSize(new Dimension(200,30));
        nameText.setText(selectedClient.getName());
        jPanel.add(nameLabel);
        jPanel.add(nameText);

        //性别
        Vector<String> sexData=new Vector<>();
        sexData.addElement("男");
        sexData.addElement("女");
        sex=new JComboBox(sexData);
        sexLabel.setPreferredSize(new Dimension(80,30));
        sex.setPreferredSize(new Dimension(200,30));;
        jPanel.add(sexLabel);
        jPanel.add(sex);


        phoneLabel.setPreferredSize(new Dimension(80,30));
        phoneText.setPreferredSize(new Dimension(200,30));
        phoneText.setText(selectedClient.getPhone());
        jPanel.add(phoneLabel);
        jPanel.add(phoneText);

        addressLabel.setPreferredSize(new Dimension(80,30));
        addressText.setPreferredSize(new Dimension(200,30));
        addressText.setText(String.valueOf(selectedClient.getAddress()));
        jPanel.add(addressLabel);
        jPanel.add(addressText);

        ideal_houseLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(ideal_houseLabel);
        Vector<String>data= houseDo.getHouseLayouts();
        ideal_house=new JComboBox(data);
        ideal_house.setPreferredSize(new Dimension(200,30));
        jPanel.add(ideal_house);

        select_numLabel.setPreferredSize(new Dimension(80,30));
        select_numText.setPreferredSize(new Dimension(200,30));
        select_numText.setText(String.valueOf(selectedClient.getSelect_num()));
        jPanel.add(select_numLabel);
        jPanel.add(select_numText);

        updateBtn.addActionListener(updateClientViewHandler);
        jPanel.add(updateBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    /**
     * 获取修改后的学生对象
     * @return
     */
    public ClientDo buildUpdateStudent() {
        ClientDo clientDo =new ClientDo();
        clientDo.setId(Integer.parseInt(idText.getText()));
        clientDo.setName(nameText.getText());
        clientDo.setSex(sex.getSelectedItem().toString());
        clientDo.setPhone(phoneText.getText());
        clientDo.setAddress(addressText.getText());   //!!!注意 JTextfield获取数据,需要用包装类转换
        clientDo.setIdea_house(ideal_house.getSelectedItem().toString());
        clientDo.setSelect_num(select_numText.getText());
        return clientDo;
    }

}
